package org.spring.core.type.classreading;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.objectweb.asm.ClassReader;
import org.spring.core.io.Resource;
import org.spring.core.type.AnnotationMetaData;
import org.spring.core.type.ClassMetadata;
import org.spring.core.type.MetaDataReader;

public class SimpleMetaDataReader implements MetaDataReader {

	private final Resource resource;
	
	private final ClassMetadata classMetadata;
	
	private final AnnotationMetaData annotationMetadata;
	
	public SimpleMetaDataReader(Resource resource) throws IOException {
		InputStream is = new BufferedInputStream(resource.getInputStream());
		ClassReader classReader;
		
		try {
			classReader = new ClassReader(is);
		}
		finally {
			is.close();
		}

		AnnotationMetaDataReadingVisitor visitor = new AnnotationMetaDataReadingVisitor();
		classReader.accept(visitor, ClassReader.SKIP_DEBUG);

		this.annotationMetadata = visitor;
		this.classMetadata = visitor;
		this.resource = resource;
	}
	

	public Resource getResource() {
		return this.resource;
	}

	public ClassMetadata getClassMetadata() {
		return this.classMetadata;
	}

	public AnnotationMetaData getAnnotationMetadata() {
		return this.annotationMetadata;
	}

}
